<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="源代码" prop="sourceCode">
        <el-input v-model="formData.sourceCode" placeholder="请输入源代码" />
      </el-form-item>
      <el-form-item label="编程语言ID" prop="languageId">
        <el-input v-model="formData.languageId" placeholder="请输入编程语言ID" />
      </el-form-item>
      <el-form-item label="标准输入数据" prop="stdin">
        <el-input v-model="formData.stdin" placeholder="请输入标准输入数据" />
      </el-form-item>
      <el-form-item label="预期输出结果" prop="expectedOutput">
        <el-input v-model="formData.expectedOutput" placeholder="请输入预期输出结果" />
      </el-form-item>
      <el-form-item label="标准输出结果" prop="stdout">
        <el-input v-model="formData.stdout" placeholder="请输入标准输出结果" />
      </el-form-item>
      <el-form-item label="运行状态ID" prop="statusId">
        <el-input v-model="formData.statusId" placeholder="请输入运行状态ID" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker
          v-model="formData.createdAt"
          type="date"
          value-format="x"
          placeholder="选择创建时间"
        />
      </el-form-item>
      <el-form-item label="完成时间" prop="finishedAt">
        <el-date-picker
          v-model="formData.finishedAt"
          type="date"
          value-format="x"
          placeholder="选择完成时间"
        />
      </el-form-item>
      <el-form-item label="运行耗时(秒)" prop="time">
        <el-date-picker
          v-model="formData.time"
          type="date"
          value-format="x"
          placeholder="选择运行耗时(秒)"
        />
      </el-form-item>
      <el-form-item label="内存使用量(KB)" prop="memory">
        <el-input v-model="formData.memory" placeholder="请输入内存使用量(KB)" />
      </el-form-item>
      <el-form-item label="标准错误输出" prop="stderr">
        <el-input v-model="formData.stderr" placeholder="请输入标准错误输出" />
      </el-form-item>
      <el-form-item label="提交令牌" prop="token">
        <el-input v-model="formData.token" placeholder="请输入提交令牌" />
      </el-form-item>
      <el-form-item label="运行次数" prop="numberOfRuns">
        <el-input v-model="formData.numberOfRuns" placeholder="请输入运行次数" />
      </el-form-item>
      <el-form-item label="CPU时间限制(秒)" prop="cpuTimeLimit">
        <el-input v-model="formData.cpuTimeLimit" placeholder="请输入CPU时间限制(秒)" />
      </el-form-item>
      <el-form-item label="CPU额外时间(秒)" prop="cpuExtraTime">
        <el-date-picker
          v-model="formData.cpuExtraTime"
          type="date"
          value-format="x"
          placeholder="选择CPU额外时间(秒)"
        />
      </el-form-item>
      <el-form-item label="墙钟时间限制(秒)" prop="wallTimeLimit">
        <el-input v-model="formData.wallTimeLimit" placeholder="请输入墙钟时间限制(秒)" />
      </el-form-item>
      <el-form-item label="内存限制(KB)" prop="memoryLimit">
        <el-input v-model="formData.memoryLimit" placeholder="请输入内存限制(KB)" />
      </el-form-item>
      <el-form-item label="栈空间限制(KB)" prop="stackLimit">
        <el-input v-model="formData.stackLimit" placeholder="请输入栈空间限制(KB)" />
      </el-form-item>
      <el-form-item label="最大进程/线程数" prop="maxProcessesAndOrThreads">
        <el-input v-model="formData.maxProcessesAndOrThreads" placeholder="请输入最大进程/线程数" />
      </el-form-item>
      <el-form-item label="启用每进程/线程时间限制" prop="enablePerProcessAndThreadTimeLimit">
        <el-radio-group v-model="formData.enablePerProcessAndThreadTimeLimit">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="启用每进程/线程内存限制" prop="enablePerProcessAndThreadMemoryLimit">
        <el-radio-group v-model="formData.enablePerProcessAndThreadMemoryLimit">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="最大文件大小(KB)" prop="maxFileSize">
        <el-input v-model="formData.maxFileSize" placeholder="请输入最大文件大小(KB)" />
      </el-form-item>
      <el-form-item label="编译输出信息" prop="compileOutput">
        <el-input v-model="formData.compileOutput" placeholder="请输入编译输出信息" />
      </el-form-item>
      <el-form-item label="退出代码" prop="exitCode">
        <el-input v-model="formData.exitCode" placeholder="请输入退出代码" />
      </el-form-item>
      <el-form-item label="退出信号" prop="exitSignal">
        <el-input v-model="formData.exitSignal" placeholder="请输入退出信号" />
      </el-form-item>
      <el-form-item label="消息提示" prop="message">
        <el-input v-model="formData.message" placeholder="请输入消息提示" />
      </el-form-item>
      <el-form-item label="墙钟时间(秒)" prop="wallTime">
        <el-date-picker
          v-model="formData.wallTime"
          type="date"
          value-format="x"
          placeholder="选择墙钟时间(秒)"
        />
      </el-form-item>
      <el-form-item label="编译器选项" prop="compilerOptions">
        <el-input v-model="formData.compilerOptions" placeholder="请输入编译器选项" />
      </el-form-item>
      <el-form-item label="命令行参数" prop="commandLineArguments">
        <el-input v-model="formData.commandLineArguments" placeholder="请输入命令行参数" />
      </el-form-item>
      <el-form-item label="重定向stderr到stdout" prop="redirectStderrToStdout">
        <el-radio-group v-model="formData.redirectStderrToStdout">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="回调URL" prop="callbackUrl">
        <el-input v-model="formData.callbackUrl" placeholder="请输入回调URL" />
      </el-form-item>
      <el-form-item label="附加文件(二进制)" prop="additionalFiles">
        <el-input v-model="formData.additionalFiles" placeholder="请输入附加文件(二进制)" />
      </el-form-item>
      <el-form-item label="启用网络访问" prop="enableNetwork">
        <el-radio-group v-model="formData.enableNetwork">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="输出文件列表(逗号分隔)" prop="outfiles">
        <el-input v-model="formData.outfiles" placeholder="请输入输出文件列表(逗号分隔)" />
      </el-form-item> -->
      <el-form-item label="API文档附加ID，关联API文档信息" prop="plusid">
        <el-input v-model="formData.plusid" placeholder="请输入API文档附加ID，关联API文档信息" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { SubmissionApi, SubmissionVO } from '@/api/plus/submission'

/** 代码提交 表单 */
defineOptions({ name: 'SubmissionForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  sourceCode: undefined,
  languageId: undefined,
  stdin: undefined,
  expectedOutput: undefined,
  stdout: undefined,
  statusId: undefined,
  createdAt: undefined,
  finishedAt: undefined,
  time: undefined,
  memory: undefined,
  stderr: undefined,
  token: undefined,
  numberOfRuns: undefined,
  cpuTimeLimit: undefined,
  cpuExtraTime: undefined,
  wallTimeLimit: undefined,
  memoryLimit: undefined,
  stackLimit: undefined,
  maxProcessesAndOrThreads: undefined,
  enablePerProcessAndThreadTimeLimit: undefined,
  enablePerProcessAndThreadMemoryLimit: undefined,
  maxFileSize: undefined,
  compileOutput: undefined,
  exitCode: undefined,
  exitSignal: undefined,
  message: undefined,
  wallTime: undefined,
  compilerOptions: undefined,
  commandLineArguments: undefined,
  redirectStderrToStdout: undefined,
  callbackUrl: undefined,
  additionalFiles: undefined,
  enableNetwork: undefined,
  outfiles: undefined,
  plusid: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SubmissionApi.getSubmission(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SubmissionVO
    if (formType.value === 'create') {
      await SubmissionApi.createSubmission(data)
      message.success(t('common.createSuccess'))
    } else {
      await SubmissionApi.updateSubmission(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    sourceCode: undefined,
    languageId: undefined,
    stdin: undefined,
    expectedOutput: undefined,
    stdout: undefined,
    statusId: undefined,
    createdAt: undefined,
    finishedAt: undefined,
    time: undefined,
    memory: undefined,
    stderr: undefined,
    token: undefined,
    numberOfRuns: undefined,
    cpuTimeLimit: undefined,
    cpuExtraTime: undefined,
    wallTimeLimit: undefined,
    memoryLimit: undefined,
    stackLimit: undefined,
    maxProcessesAndOrThreads: undefined,
    enablePerProcessAndThreadTimeLimit: undefined,
    enablePerProcessAndThreadMemoryLimit: undefined,
    maxFileSize: undefined,
    compileOutput: undefined,
    exitCode: undefined,
    exitSignal: undefined,
    message: undefined,
    wallTime: undefined,
    compilerOptions: undefined,
    commandLineArguments: undefined,
    redirectStderrToStdout: undefined,
    callbackUrl: undefined,
    additionalFiles: undefined,
    enableNetwork: undefined,
    outfiles: undefined,
    plusid: undefined
  }
  formRef.value?.resetFields()
}
</script>